package com.booking.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.booking.ImageFetchable;
import com.booking.R;
import com.booking.entity.BarberShopItem;
import com.booking.entity.ListItem;
import com.booking.entity.ListItem.ItemType;

public class PlacesListAdapter extends BaseAdapter {
	private List<ListItem> mItems;
	private LayoutInflater mInflater;
	private ImageFetchable fetcher;

	public PlacesListAdapter(Context context) {
		mItems = new ArrayList<ListItem>();
		fetcher = (ImageFetchable) context;
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	static abstract class ViewHolder {

		public ViewHolder(ItemType type) {
			this.type = type;
		}

		final ItemType type;

		abstract void initView(View view);

		abstract void setData(ListItem item, ImageFetchable fetchable);
	}

	@Override
	public int getCount() {
		return mItems.size();
	}

	@Override
	public ListItem getItem(int position) {
		return mItems.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setItems(List<ListItem> items) {
		mItems.clear();
		mItems.addAll(items);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ListItem item = getItem(position);
		ViewHolder holder;
		if (convertView == null) {
			convertView = mInflater.inflate(item.getType().getId(), parent,
					false);
			holder = createViewHolder(item.getType(), convertView);
		} else {
			holder = (ViewHolder) convertView.getTag();
			if (holder.type != item.getType()) {
				convertView = mInflater.inflate(item.getType().getId(), parent,
						false);
				holder = createViewHolder(item.getType(), convertView);
			}
		}

		holder.setData(item, fetcher);

		return convertView;
	}

	private ViewHolder createViewHolder(ItemType type, View convertView) {
		ViewHolder holder = null;
		switch (type) {
		case BARBERSHOP_ITEM:
			holder = new BarbershopViewHolder();
			break;
		default:

			break;
		}
		if (holder != null) {
			holder.initView(convertView);
			convertView.setTag(holder);
		}
		return holder;
	}

	class BarbershopViewHolder extends ViewHolder {
		ImageView photoView;
		TextView nameView;
		TextView addressView;
		TextView timeView;
		TextView distanceView;

		public BarbershopViewHolder() {
			super(ItemType.BARBERSHOP_ITEM);
		}

		@Override
		void initView(View view) {
			photoView = (ImageView) view.findViewById(R.id.photo);
			nameView = (TextView) view.findViewById(R.id.place_name);
			addressView = (TextView) view.findViewById(R.id.place_address);
			timeView = (TextView) view.findViewById(R.id.place_time);
			distanceView = (TextView) view.findViewById(R.id.place_distance);
		}

		@Override
		void setData(ListItem item, ImageFetchable fetchable) {
			BarberShopItem barberShopItem = (BarberShopItem) item;
			nameView.setText(barberShopItem.getName());
			addressView.setText(barberShopItem.getAddress());
			timeView.setText(barberShopItem.getTime());
			distanceView.setText(barberShopItem.getDistance());
			fetchable.getPlacePhotoFetcher().loadImage(barberShopItem.getUrl(),
					photoView);
		}

	}
}
